package com.pe.pgn.clubpgn.util;

import java.awt.Color;

import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

public class ITextPDFWriter {

	public static PdfPCell getPdfPCell(Font font, Color color, String text, float widthBorder, Color borderColor, 
			boolean useBorderPadding, boolean isLast){
		
		Chunk subtitle = new Chunk(text, font);
		Paragraph paragraph = new Paragraph(subtitle);
		
		PdfPCell pdfPCell = new PdfPCell(paragraph);
		
		pdfPCell.setBorder(Rectangle.NO_BORDER);
		pdfPCell.setBackgroundColor(color);
		
		pdfPCell.setBorderWidthLeft(widthBorder);
		pdfPCell.setBorderColorLeft(borderColor);
		
		pdfPCell.setBorderWidthRight(widthBorder);
		pdfPCell.setBorderColorRight(borderColor);
		
		if(isLast){
			pdfPCell.setBorderWidthBottom(widthBorder);
			pdfPCell.setBorderColorBottom(borderColor);
		}
				
		return pdfPCell;	
	}
	
	public static HeaderFooter createFooter(String footerMessage, float footerFontSize){
		
		Font footerFont = new Font();
		footerFont.setSize(footerFontSize);
		            
        // headers and footers must be added before the document is opened
        HeaderFooter footer = new HeaderFooter(new Phrase(footerMessage, footerFont), false); //Colocar true para numeracion
        footer.setBorder(Rectangle.NO_BORDER);
        footer.setAlignment(Element.ALIGN_CENTER);
        
        return footer;
	}
	
	public static Paragraph createTitleCenterParagraph(String title, float titleSize){
		
		Font titleFont = new Font();
		titleFont.setSize(titleSize);
		titleFont.setStyle(Font.BOLD);
		
		Chunk chunk = new Chunk(title, titleFont);
//		title.setUnderline(new Color(0x000000), 2.0f, 0.0f, -5.0f, 0.0f, PdfContentByte.LINE_CAP_PROJECTING_SQUARE);			
		Paragraph paragraph = new Paragraph(chunk);
		paragraph.setAlignment(Element.ALIGN_CENTER);
		
		return paragraph;
	}
	
	public static Document addEmptyParagraph(Document document){
		
		try {
			document.add(new Paragraph("    "));
		} catch (DocumentException e) {
			System.out.println(" Method addEmptyParagraph error = "+e.getMessage());
		}
		return document;
	}
	
	public static PdfPTable createPdfPTable(int numberColumns, boolean noBorder){
		PdfPTable pdfPTable = new PdfPTable(numberColumns);
		
		if(noBorder){
			pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);	
		}

		return pdfPTable;
	}
	
	public static PdfPTable createParagraphForTable(Font font, String label, Color labelBackground, String value,  
			PdfPTable pdfPTable, boolean noBorder){
		
		Chunk chunk = new Chunk(label, font);
		
		if(labelBackground != null){
			chunk.setBackground(labelBackground);	
		}
		
		Paragraph paragraph = new Paragraph(chunk);
		
		chunk = new Chunk(value, font);
		paragraph.add(chunk);
					
		PdfPCell pdfPCell = new PdfPCell(paragraph);
		
		if(noBorder){
			pdfPCell.setBorder(Rectangle.NO_BORDER);
		}
		pdfPTable.addCell(pdfPCell);
		
		return pdfPTable;
	}
		
	public static PdfPTable createEmptyParagraphForTable(PdfPTable pdfPTable, boolean noBorder){
		
		PdfPCell pdfPCell = new PdfPCell(new Paragraph("    "));
		pdfPCell.setBorder(Rectangle.NO_BORDER);
		pdfPTable.addCell(pdfPCell);
		
		return pdfPTable;
	}
	
	public static PdfPTable createPdfPCellWithImage(PdfPTable pdfPTable, Image jpg, float padding, 
			Color backgroundColor, Color borderColor, float widthBorder){
		
		PdfPCell pdfCellImage = new PdfPCell(jpg, false);
		pdfCellImage.setPadding(padding);
		pdfCellImage.setHorizontalAlignment(Element.ALIGN_CENTER);
		pdfCellImage.setVerticalAlignment(Element.ALIGN_CENTER);
		pdfCellImage.setBorder(Rectangle.NO_BORDER);
		pdfCellImage.setBackgroundColor(backgroundColor);
		pdfCellImage.setBorderColorTop(borderColor);
		pdfCellImage.setBorderWidthTop(widthBorder);					
		pdfCellImage.setBorderWidthLeft(widthBorder);
		pdfCellImage.setBorderColorLeft(borderColor);
		pdfCellImage.setBorderWidthRight(widthBorder);
		pdfCellImage.setBorderColorRight(borderColor);
		pdfPTable.addCell(pdfCellImage);
		
		return pdfPTable;
	}	
}
